package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterRoomDao;
import com.vf.admin.miaodong.dao.AfterUserDao;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterRoomService;
import com.vf.admin.miaodong.service.AfterUserService;
import com.vf.admin.utils.WYIM.WYMsg;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广场管理
 */
@CrossOrigin
@Controller
@RequestMapping("/after/square")
public class AfterSquareController extends BaseController {
    @Autowired
    AfterUserService afterUserService;

    @Autowired
    AfterRoomService afterRoomService;

    /**
     * 分页获取成员列表
     */
    @RequestMapping("/list")
    public String list(SplitPageBean pageBean, Map<String, Object> parMap){
        try {
            String res = WYMsg.getRoomUser("162245096", "1", "0", "20");
            org.json.JSONObject jsonObject = new org.json.JSONObject(res);
            Integer code = (Integer) jsonObject.get("code");
            List<Map<String, Object>> list = new ArrayList<>();
            if (code == 200) {
                org.json.JSONObject info = jsonObject.getJSONObject("desc");
                JSONArray data = info.getJSONArray("data");
                if (data.length() > 0) {
                    for (int i = 0; i < data.length(); i++) {
                        Map<String, Object> mapp = new HashMap<>();
                        org.json.JSONObject o = data.getJSONObject(i);
                        String admin = (String) o.get("type");
                        mapp.put("nick", o.get("nick"));
                        mapp.put("accid", o.get("accid"));
                        mapp.put("enterTime", o.get("enterTime"));
                        mapp.put("type", o.get("type"));
                        mapp.put("onlineStat", o.get("onlineStat"));
                        mapp.put("avator", o.get("avator"));
                        mapp.put("roomid", o.get("roomid"));
                        AppUser userByState = afterUserService.getUserByCode((String) o.get("accid"));
                        mapp.put("user", userByState);
                        list.add(mapp);
                    }
                    request.setAttribute("list", list);
                    request.setAttribute("sm", parMap);
                    request.setAttribute("splitPage", pageBean);
                    return this.sucess("/web/after/squareManager/list");
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }
    /**
     * 全体禁言跳转
     *
     * @return
     */
    @RequestMapping("/qtJin")
    public String qtJin() {
        try {
            return this.sucess("/web/after/squareManager/qtJin");
        } catch (Exception e) {
            return this.error("全体禁言跳转异常");
        }
    }
    /**
     * 置顶禁言跳转
     *
     * @return
     */
    @RequestMapping("/zdJin")
    public String zdJin() {
        try {
            return this.sucess("/web/after/squareManager/zdJin");
        } catch (Exception e) {
            return this.error("指定禁言跳转异常");
        }
    }

    /**
     * 将聊天室整体禁言
     */
    @RequestMapping("/muteRoomC")
    @ResponseBody
    public Result muteRoomC(String mute) {
        if (StringUtils.isEmpty(mute)) {
            return errorJSON("mute不能为空");
        }
        //设置禁言状态  true禁言   false解禁
        try {
            Map<String, Object> map = WYMsg.muteRoom("162245096", "1245860850", mute);

            if ((Integer) (map.get("code")) != 200) {
                if (mute.equals("true")) {
                    return errorJSON("设置禁言失败");
                }
                if (mute.equals("false")) {
                    return errorJSON("设置解禁失败");
                }
            }
            return sucess();
        } catch (Exception e) {
            return errorJSON(e.getMessage());
        }
    }

    /**
     * 将聊天室某个用户禁言
     */
    @RequestMapping("/temporaryMuteC")
    @ResponseBody
    public Result temporaryMuteC(String target, Integer muteDuration) {
        Map<String, Object> parMap=new HashMap<>(16);
        parMap.put("target",target);
        parMap.put("muteDuration",muteDuration);
        String userid = "1245860850";
        if (StringUtils.isEmpty(userid)) {
            return errorJSON("userid不能为空");
        }
        if (StringUtils.isEmpty(target)) {//被禁言的目标账号accid
            return errorJSON("target不能为空");
        }
        if (StringUtils.isEmpty(muteDuration)) {//0:解除禁言;>0设置禁言的秒数，不能超过2592000秒(30天)
            return errorJSON("muteDuration不能为空");
        }
        AppUser id = afterUserService.get(Integer.valueOf(target));
        if (id == null) {
            return errorJSON("用户不存在");
        }
        try {
            Map<String, Object> map = WYMsg.temporaryMute("162245096", userid, id.getCode(), muteDuration.toString());
            if ((Integer) (map.get("code")) != 200) {
                return errorJSON("操作失败");
            }
            return sucess();
        } catch (Exception e) {
            e.printStackTrace();
            return errorJSON("请输入有效的用户信息");
        }
    }

}
